<template>
  <div class="login-container">
    <el-row :gutter="20" class="header">

      <el-col :span="14" offset="3">
        <h1 class="websitename">小黑教育-- 致敬全国所有教师</h1>
      </el-col>
    </el-row>

    <div class="loginblock login-form">
      <el-carousel autoplay="false" indicator-position="none">
        <el-carousel-item>
          <el-form
            autoComplete="on"
            :model="loginForm"
            :rules="loginRules"
            ref="loginForm"
            label-position="left"
            label-width="0px"
            class="card-box"
          >
            <h3 class="title">教师登录</h3>
            <el-form-item prop="name">
              <span class="svg-container svg-container_login">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                name="name"
                type="text"
                v-model="loginForm.name"
                autoComplete="on"
                placeholder="name"
              />
            </el-form-item>
            <el-form-item prop="password">
              <span class="svg-container">
                <svg-icon icon-class="password"></svg-icon>
              </span>
              <el-input
                name="password"
                :type="pwdType"
                @keyup.enter.native="handleLogin"
                v-model="loginForm.password"
                autoComplete="on"
                placeholder="password"
              ></el-input>
              <span class="show-pwd" @click="showPwd"
                ><svg-icon icon-class="eye"
              /></span>
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                style="width: 100%"
                :loading="loading"
                @click.native.prevent="handleLogin"
              >
                Sign in
              </el-button>
            </el-form-item>
          </el-form>
        </el-carousel-item>
      </el-carousel>
    </div>
    <vue-particles
      class="bg"
      color="#dedede"
      :particleOpacity="0.7"
      :particlesNumber="80"
      shapeType="circle"
      :particleSize="4"
      linesColor="#dedede"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.4"
      :linesDistance="150"
      :moveSpeed="3"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
    >
    </vue-particles>
  </div>
</template>

<script>
import { login, logout } from "@/api/teacher";
export default {
  name: "login",
  data() {
    const validatename = (rule, value, callback) => {
      callback();
    };
    const validatePass = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("密码不能小于5位"));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        name: "admin",
        password: "admin",
      },
      loginRules: {
        name: [{ required: true, trigger: "blur", validator: validatename }],
        password: [
          { required: true, trigger: "blur", validator: validatePass },
        ],
      },
      loading: false,
      pwdType: "password",
    };
  },
  methods: {
    showPwd() {
      if (this.pwdType === "password") {
        this.pwdType = "";
      } else {
        this.pwdType = "password";
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          login(this.loginForm.name, this.loginForm.password)
            .then((response) => {
              localStorage.setItem("logininfo", JSON.stringify(response));
              this.loading = false;
              this.$router.push({ path: "/teacher" });
            })
            .catch((error) => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #979797;
$dark_gray: #6c8ac0;
$light_gray: rgb(250, 250, 250);

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;
  input:-webkit-autofill {
    -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
    -webkit-text-fill-color: #fff !important;
  }
  .loginform {
    float: right;
    margin-right: 300px;
  }

  .header {
    width: 100%;
    height: 80px;
    padding: 10px;
    background-color: rgb(255, 255, 255);

    .bannerimg {
      height: 60px;
    }
    .websitename {
      vertical-align: center;
      color: #8a8e92;
    }
  }
  .loginblock {
  }
  .bgimg {
    position: fixed;
    left: 0;
    right: 0;
    z-index: -1;
  }
  input {
    background: transparent;
    border: 0px;
    -webkit-appearance: none;
    border-radius: 0px;
    padding: 12px 5px 12px 15px;
    color: $light_gray;
    height: 47px;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title {
    font-size: 26px;
    font-weight: 400;
    color: $light_gray;
    margin: 0px auto 40px auto;
    text-align: center;
    font-weight: bold;
  }
  .login-form {
    float: right;
    background-color: #8a8e92;
    margin-right: 200px;
    margin-top: 100px;
    width: 400px;
    padding: 35px 35px 15px 35px;
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
</style>
